<template>
	<view>
		<view class="questions-nav">
			<view class="questions-nav-title">当前:{{questionskeys+1}}/{{questionscount}}</view>
			<view class="questions-nav-right">{{formatTime(timer)}}</view>
			<button class="sharebutton questions-nav-explain share" open-type="share">
				<image src="../../static/questions/share.png"></image>
				<view>一键分享</view>
			</button>
			<view class="questions-nav-explain" @click="explainChange(1)">
				<image src="../../static/questions/shuo.png"></image>
				<view>说明</view>
			</view>
		</view>
		<All :info="info"   :bijishow="bijishow" @userinputChange="userinputChange"></All>
		<Function
			:tskeys = "tskeys"
			:index = "index"  
			:index1 ="index1"
			:tshintnumber="tshintnumber"
			@bijiChange = "bijiChange"
			@hintChange="hintChange"
			@erasureChange="erasureChange"
			@revocationChange="revocationChange"
			@advertisingChange = "advertisingChange" 
		></Function> 
		
		<Biji v-if="bijishow == 1"
			@bijiNumChange="bijiNumChange" 
			:info="info"
			:index="index"
			:index1="index1"
			:bijireplace="bijireplace"
		></Biji>
		<Imports v-if="bijishow == 0"  @numberChange="numberChange" :info="info"></Imports>
	
		<Bottom @bottomChange="bottomChange" @submitChange="submitChange"></Bottom>
	</view>
</template>

<script>
	import All  from "../../components/cell/all.vue"
	import  Biji  from "../../components/cell/biji.vue"
	
	import Function from  "../../components/exam/function.vue"
	import Imports from  "../../components/cell/import.vue"
	import Popup from  "../../components/cell/popup.vue"
	import Explain from "../../components/cell/explain.vue"
	import Bottom from "../../components/exam/bottom.vue"
	
	import {Calculate,numSub,cachequestions} from '../../common/uits/questions.js'
	export default {
		components:{
			Biji,All,
			Function,Imports,Popup,Explain,Bottom
		},
		data() {
			return {
				bid  : '',
				questionslist : [],
				questionscount: [],
				questionskeys : 0 ,
				info          : [],
				questions_id  : '',
				timer		  : 0,
				Intervaltime  : '',
				bijishow      : 0 , //笔记
				bijireplace   : 0,  // 笔记  更换笔记的时候 +1；
				tskeys        : '',  
				index  		  : '',
				index1 		  : '',
				hintnumber: 0,  //提示次数  传入后端
				tshintnumber: 0,  //提示次数 前端使用
				user_id    : uni.getStorageSync("user_id"),
				newlist    : [],
				key        : ''
			}
		},
		onLoad(e) {
			this.bid     = e.bid;
			this.query   = e;
			this.key     = this.user_id+'examlist'+this.bid;
			this.tskeys  = this.user_id+'exam'+this.bid;
			this.login();
		},
		methods: {
			numberChange(e){
				this.number  = e;
				if(this.info['cid'] == 12 || this.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
					this.info.body['rule'][this.index][this.index1]['uservalue']  = this.number;
				}else{
					this.info.body[this.index][this.index1]['uservalue']  = this.number;
				}
				var lists = {
					'questionskeys': this.questionskeys,
					'questionslist':this.questionslist,
					'newlist':this.newlist,
				};
				
				uni.setStorageSync(this.key,JSON.stringify(lists));
			},
			bijiChange(e){
				this.bijishow =  e;
			},
			bijiNumChange(e){
				console.log(new Date().toLocaleString());
				if(this.info['cid'] == 12 || this.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
					var  biji  = this.info.body['rule'][this.index][this.index1]['biji'];
					if(biji.indexOf(e) == -1){
						biji.push(e);
					}else{
						biji.splice(biji.indexOf(e),1);
					}
					this.info.body['rule'][this.index][this.index1]['biji']  =biji;
				}else{
					var  biji  = this.info.body[this.index][this.index1]['biji'];
					if(biji.indexOf(e) == -1){
						biji.push(e);
					}else{
						biji.splice(biji.indexOf(e),1);
					}
					this.info.body[this.index][this.index1]['biji']  = biji;
					
				}
				var lists = {
					'questionskeys': this.questionskeys,
					'questionslist':this.questionslist,
					'newlist':this.newlist,
				};
				
				uni.setStorageSync(this.key,JSON.stringify(lists));
				console.log(new Date().toLocaleString());
			},
			hintChange(e){   //提示
				this.hintnumber++;
				if(this.info['cid'] == 12 || this.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
					this.info.body['rule'][this.index][this.index1]['uservalue']  = this.info.body['rule'][this.index][this.index1]['value'];
				}else{
					this.info.body[this.index][this.index1]['uservalue']  = this.info.body[this.index][this.index1]['value'];
				}
				
			},
			bottomChange(e){
				let  that  = this;
				console.log(that.questionskeys);
				console.log( that.newlist);
				if(e == 1){
					if(that.questionskeys+1  < that.questionscount){
						that.questionskeys++;
						that.questions_id   =  that.questionslist[that.questionskeys]['id'];
					}else{
						uni.showToast({
							title: '已经最后一题啦~',
							icon: "none" ,
						});return;
					}
				}else if(e == 0){
					if(that.questionskeys > 0){
						that.questionskeys--;
						that.questions_id   =  that.questionslist[that.questionskeys]['id'];
					}else{
						uni.showToast({
							title: '已经是第一题啦~',
							icon: "none" ,
						});return;
					}
				}
				var newlistkey = that.newlist[that.questionskeys];
				
				if(newlistkey){
					that.info   = that.newlist[that.questionskeys];
				}else{
					that.getinfo();
				}
				uni.setNavigationBarTitle({
				    title: that.info.name
				})
			},
			submitChange(e){
				let that = this;
				uni.showModal({
					title: '提示',
					content: '您确定要提交考试吗！',
					success: function (res) {
						if (res.confirm) {
							var parmes  = {
								'bid'   : that.bid,
								'body'   : JSON.stringify(that.newlist),
								'timer'   :that.timer,
								'hintnumber':that.hintnumber
							}
							
							that.$exam.submit(parmes).then(res=>{
								uni.showToast({
									title: res.msg,
									icon: "none" ,
								});
								if(res.code == 1){
									uni.setStorageSync(that.key,'');
									uni.setStorageSync(that.tskeys,'');
									clearInterval(that.Intervaltime);
									uni.redirectTo({
									  url:'/pages/bak/exam_details?id='+that.bid
									});
									
								}
							})
						}
					},
				})
			},
			userinputChange(e){
				this.index  = e.index;
				this.index1  = e.index1;
				if(this.info['cid'] == 12 || this.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
					this.info.body['rule'][e.index][e.index1]['ticolor']  = '';
				}else{
					this.info.body[e.index][e.index1]['ticolor']  = '';
				}
				if(this.bijishow == 1) {
					this.bijireplace++;
				}	
			},
			getall(){
				let that = this;
				that.$questions.getexamlist({'bid':that.bid}).then(res=>{
					that.questionslist  = res.data.list;
					that.questionscount = res.data.count;
					that.questions_id   = res.data.list[0]['id'];
		
					var getItem = uni.getStorageSync(that.key);
					if(getItem != null &&  getItem.length >0){
						getItem  =JSON.parse(getItem);
						console.log('cache');
						
						that.questionslist = getItem.questionslist;
						that.newlist = getItem.newlist;
						that.info    = that.newlist[0];
						uni.setNavigationBarTitle({
						    title: that.info.name
						})
						console.log(that.newlist);
					}else{
						that.getinfo()
					}
					
				})
			},
			getinfo(){
				let  that = this;
				that.$questions.getdetails({'ids':that.questions_id}).then(res=>{
					that.info  = res.data.info;
					if(that.info['cid'] == 12 || that.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
						var  body  = that.info.body;
						for(var i = 0 ; i < body['rule'].length; i++){
							for(var j = 0; j < body['rule'][i].length;j++){
								body['rule'][i][j]['biji']   = [];
								if(body['rule'][i][j]['type']  == 1 ){
									body['rule'][i][j]['ticolor']  =  '';
								}
							}
						}
						
					}else{
						var  body  = that.info.body;
						for(var i = 0 ; i < body.length; i++){
							for(var j = 0; j < body[i].length;j++){
								body[i][j]['biji']   = [];
								if(body[i][j]['type']  == 1 ){
									body[i][j]['ticolor']  =  '';
									
								}
							}
						}
					}
					that.info.body = body;
					that.newlist=that.newlist.concat(that.info);
					console.log(that.newlist);
					that.showhide  = 1;
					uni.hideToast();
					uni.setNavigationBarTitle({
					    title: that.info.name
					})
				})
			},
			gettshintnumber(){
				let that = this;
				if(!uni.getStorageSync(that.tskeys)){
					that.$api.getConfig({'keys':'hintnumber'}).then(res=>{
						that.tshintnumber  = res.data.value;
						uni.setStorageSync(that.tskeys,that.hintnumber);
					})
				}else{
					console.log('==========');
					that.tshintnumber  = uni.getStorageSync(that.tskeys);
					console.log(that.tshintnumber);
				}
			},
			formatTime(time) {
			    // 将时间格式化为 hh:mm:ss 的形式
			    let hour = Math.floor(time / 3600);
				let minute = Math.floor((time - hour * 3600) / 60);
			    let second = Math.floor(time - hour * 3600 - minute * 60);
			    return `${hour.toString().padStart(2, '0')}:${minute
			        .toString()
			        .padStart(2, '0')}:${second.toString().padStart(2, '0')}`;
			},
			login(){
				let that = this;
				if(!uni.getStorageSync("user_id")){
					wx.login({
					    success(res) {
					        if (res.code) {
								that.$user.login({'code':res.code}).then(res=>{
									if(res.code == 1){
										uni.setStorageSync("user_id",res.data.user_id);
										that.getall();
										that.gettshintnumber();
									}
								})
					        } else {
								console.log('登录失败！' + res.errMsg)
							}
					    }
					});
				}else{
					this.getall();
					this.gettshintnumber();
				}
			}
		},
		mounted() {
			this.Intervaltime = setInterval(() => {
		      this.timer++;
		    }, 1000);
		},
	}
</script>

<style>

</style>
